package sol.leetcode;

import java.util.Arrays;

/**
 * Rotate Image
 */
public class Issue048 {
    public void rotate(int[][] matrix) {
        int len = matrix.length;

        //按照对角线对换
        for (int i = 0; i < len; ++i)
            for (int j = i; j < len; ++j) {
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = tmp;
            }

        //左右互换
        for (int i = 0; i < len; ++i)
            for (int j = 0; j < len / 2; ++j) {
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[i][len - 1 - j];
                matrix[i][len - 1 - j] = tmp;
            }
    }

    public static void main(String[] args) {
        int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        new Issue048().rotate(matrix);
        for (int[] line : matrix)
            System.out.println(Arrays.toString(line));
    }
}
